The MSChart control is an external ActiveX control that lets you add charting capabilities to your applications. You can create two- and three-dimensional charts in different styles, including bars, lines, and pies. You have complete control over all the items in the chart, such as title, legends, footnotes, axes, data point series, and so on. You can even rotate the graph, add backdrop images to virtually any element of the chart, set up your own light sources, and place them where you want. At run time, users can select portions of the chart and move and resize them at will, if you want to provide them with this capability.
The MSChart control is undoubtedly the most complicated ActiveX control ever provided with Visual Basic. Just to give you an idea of its complexity, consider that its type library includes 47 different objects, most of which have dozens of properties, methods, and events. A detailed description of this control would require 100 pages of text, if not more. For this reason, I'll illustrate only a few of its major characteristics and provide just a few code samples. If you dare to dive into the intricacies of this hierarchy, you'll find Figure 12-12 some help in keeping you from getting lost in this maze.
The topmost object of this hierarchy is MSChart, which lets you set the general characteristics of the chart and also exposes several custom events. All the other objects in the hierarchy are dependents of MSChart.
The DataGrid object is where you store the data that you want to display graphically. The Plot object is a compound object (that is, an object with other child objects) that contains all the graphical information about the data series (color, markers, backdrop pattern, position and attributes of light sources, and so on). The Title, Legend, and Footnote objects are compound objects with similar structures and control over the features of the relevant elements of the chart (text, color, position, and so on).
Figure 12-12. The top levels of the MSChart hierarchy.
The MSChart control has the richest Property Pages dialog box among the controls provided in the Visual Basic package (see Figure 12-13), with eight tabs.
The Chart tab is where you decide which type of graphic you want to display, whether you want to stack series, and whether you show legends that explain what each data series is. These settings correspond to the ChartType, Chart3d, Stacking, and ShowLegend properties of the MSChart object.
The Axis tab is where you select the attributes of the axis of the chart: line width and color, whether the scale is displayed, and whether the scale is determined automatically by the control (the recommended setting) or manually by the programmer. In the latter case, you have to set minimum and maximum values and the frequency of divisions. Two-dimensional charts have three axes (x-axis, y-axis, and secondary y-axis), while three-dimensional charts have an additional fourth axis (z-axis). Your code can modify these properties using the Axis object, a child of the Plot object.
Figure 12-13. The Chart tab of the Property Pages dialog box of the MSChart control.
The AxisGrid tab lets you modify the style lines of axis grids; these settings correspond to the properties of the AxisGrid object, a child of the Axis object.
In the Series tab, you define how each data series should be displayed. You can hide a series (but reserve the space for it on the chart), exclude it (this also reuses its space on the chart), show its markers, and draw it on the secondary y-axis. If you are drawing a two-dimensional Line chart, you can also display statistical data, such as minimum and maximum values, mean, standard deviation, and regression. You can modify these features through code by acting on the SeriesCollection and the Series objects.
You refine the appearance of each data series in the SeriesColor tab, where you select the color and the style of the edge and the interior of each series. (The latter isn't available for Line and X-Y charts.) Your code can manipulate these properties through the DataPoint object.
All the main objects in the control—MsChart, Plot, Title, Legend, and Footnote—can have a backdrop pattern. You define the color and style of each backdrop in the Backdrop tab of the Property Pages window. The title, the legends, and the axis in your graph expose a Title, and you can set its properties in the Text and the Font tabs.
Unless you want to give users the ability to modify some key properties of your charts, you can define all the key properties at design time using the Property Pages dialog box so that at run time you only have to feed the MSChart control the actual data to be displayed. You achieve this using the DataGrid object.
You can think of the DataGrid object as a multidimensional array that holds both data and its associated labels. You define the size of the array by assigning a value to the DataGrid's RowCount and ColumnCount properties, and you define the number of labels with the RowLabelCount and ColumnLabelCount properties. For example, you might have 12 rows of data to which you add a label at every third data point:
' 12 rows of data, with a label every third row MSChart1.DataGrid.RowCount = 12 MSChart1.DataGrid.RowLabelCount = 4 ' 10 columns of data, with a label on the 1st and 6th column MSChart1.DataGrid.ColumnCount = 10 MSChart1.DataGrid.ColumnLabelCount = 2 |
Alternatively, you can set these four properties in one operation using the SetSize method:
' Syntax is: SetSize RowLabelCount, ColLabelCount, RowCount, ColCount MSChart1.DataGrid.SetSize 4, 2, 12, 10 |
You define the label text using the RowLabel and ColumnLabel properties, which accept two arguments: the row or column number and the number of the label you want to assign.
' Set a label every three years. MSChart1.DataGrid.RowLabel(1, 1) = "1988" MSChart1.DataGrid.RowLabel(4, 2) = "1991" MSChart1.DataGrid.RowLabel(7, 3) = "1994" ' And so on. |
You can set the value of individual data points using the SetData method, which has the following syntax:
MSChart.DataGrid.SetData Row, Column, Value, NullFlag |
where Value is a Double value and NullFlag is True if the data is Null. You can easily (and quickly) insert or delete rows or columns using a number of methods exposed by the DataGrid object. Among these are InsertRows, DeleteRows, InsertColumns, DeleteColumns, InsertRowLabels, DeleteRowLabels, InsertColumnLabels, and DeleteColumnLabels. You can also fill the grid with random values (useful for providing the user with visual feedback even without actual data values) with the method RandomDataFill.
You can learn a lot about the MSChart control by studying the Chrtsamp.vbp sample project that comes with Visual Basic 6 and is shown in Figure 12-14.
Figure 12-14. The sample Microsoft Chart program.
The Visual Basic package includes other controls that you might find useful in your applications. Unfortunately, I don't have room to explore all of them in depth. The controls that I have illustrated in this chapter and in Chapters 10 and 11, however, should suffice to help you create sophisticated Windows applications with great user interfaces.
This chapter concludes a series dedicated to building the user interface of your applications. Creating a good-looking and logical user interface is a requirement for a successful Windows application, but appearance isn't everything. In fact, the real value of an application is in its ability to process data, so the majority of the programs you'll write in Visual Basic have to read, write, and process the information stored in a database. In Part III of this book, I show you how to do all that in the most efficient way.